import { ChangeDetectorRef, Component, ElementRef, EventEmitter, Input, Output, output, ViewChild } from '@angular/core';
import { DesktopItemDto } from './desktop-item-dto';
import { MAT_TOOLTIP_DEFAULT_OPTIONS, MatTooltip, MatTooltipDefaultOptions, MatTooltipModule } from '@angular/material/tooltip';
import { fileInfoBaseDto } from 'ispace.core.main/dist/dto/fileInfoBaseDto';
import { CommonModule } from '@angular/common';
import { CdkMenuModule } from '@angular/cdk/menu';
import { file, folder } from 'ispace.core.main';
import { FormsModule } from '@angular/forms';
import { CdkDragHandle } from '@angular/cdk/drag-drop';
import { DriverOperationDto } from 'ispace_de';
import {ClipboardModule,Clipboard} from '@angular/cdk/clipboard';
import { DriveEnginService } from '../../service/drive-engin.service';
import { DesktopService } from '../../service/desktop.service';
import { datetimeUtil } from '../../common/utils/datetimeUtil';
import { MatDialog } from '@angular/material/dialog';
import { RenameCmpComponent } from '../rename-cmp/rename-cmp.component';

/** Custom options the configure the tooltip's default show/hide delays. */
export const myCustomTooltipDefaults: MatTooltipDefaultOptions = {
  showDelay: 3000,
  hideDelay: 10,
  touchendHideDelay: 1000,
};

@Component({
  selector: 'app-desktop-item-cmp',
  standalone: true,
  imports: [MatTooltipModule, CommonModule, CdkMenuModule, FormsModule,CdkDragHandle,ClipboardModule,],
  templateUrl: './desktop-item-cmp.component.html',
  styleUrl: './desktop-item-cmp.component.sass',

  providers: [{ provide: MAT_TOOLTIP_DEFAULT_OPTIONS, useValue: myCustomTooltipDefaults }],
})
export class DesktopItemCmpComponent {


  @Input()
  public data!: DesktopItemDto;
 
  basePath: string = "Desktop"; 

  constructor(private desktopSvc:DesktopService,private driveEngine: DriveEnginService,private clipboard:Clipboard,private dialog:MatDialog,private cdRef:ChangeDetectorRef) {
    this.desktopSvc.refreshEvent.subscribe(s=>{
      this.ngOnInit();  
    });
     
  }

  ngOnInit() {
    this.data.desc = this.formatInfo(this.data.data);
    this.init_sourceManagerOpen();
    this.open_init(); 
  }

  formatInfo(info: fileInfoBaseDto): string {
    let result = `名称：${info.name}
    大小：${info.size}
    修改时间：${datetimeUtil.format(info.modTime!,"yyyy-MM-dd HH:mm:ss")}
    `; 
    return result;
  }
@Output() onRemove = new EventEmitter<DesktopItemDto | DesktopItemDto[]>(); 

  open_op?:DriverOperationDto; 
  open_init() { 
    if (this.data.type == "folder") {
      let op =  this.ext_operations.find(s=>s.driverId==1 && s.id==1);
      if(!op)
        {
          console.error("not found");
          return;
        }
      let iconUrl = this.driveEngine.driveEngine.drivers.value.find(s=>s.id==op!.driverId)?.fileIconUrl;
      if(iconUrl)
      {
        this.data.iconUrl = iconUrl;
      }

      this.open_op = op;
    }
    else { 
      let op =  this.ext_operations.find(s=>s.name=="打开");
      if(!op)
        {
          console.error("not found");
          return;
        }
      
      // 获取打开的驱动，复制对应文件图标
      let iconUrl = this.driveEngine.driveEngine.drivers.value.find(s=>s.id==op!.driverId)?.fileIconUrl;
      if(iconUrl)
      {
        this.data.iconUrl = iconUrl;
      }
      this.open_op = op;
    }
  }
 

  open() {  
    this.driveEngine.execute(this.open_op!.driverId,this.open_op!.id,this.data.path,this.data.type,this.data); 
  }

  rename() { 
 const d = this.dialog.open(RenameCmpComponent, {
    data: this.data,
  });
  d.afterClosed().subscribe(result => {
      if (result) {
          this.desktopSvc.doRefresh();
      }
    });
}

  
copy() {
  this.clipboard.copy("$ispace copy fileBase "+ this.data.path +" "+ this.data.type);
}
shear() {
  this.clipboard.copy("$ispace shear fileBase "+ this.data.path + " "+ this.data.type);
}
  ext_operations: DriverOperationDto[] = [];
  ext_operations_current: DriverOperationDto[] = [];
  init_sourceManagerOpen() { 
    this.ext_operations=  this.driveEngine.getOperations(this.data.name,this.data.type == "folder"); 
    this.ext_operations_current = this.ext_operations.filter(s => s.name !=="打开");
    if(this.data.type == "folder") {
      this.ext_operations_current = this.ext_operations_current.filter(s => s.name !=="资源管理器打开");
      
    }
    
  }
  
  ext_operation_execute(operation: DriverOperationDto) {
       this.driveEngine.execute(operation.driverId,operation.id,this.data.path,this.data.type,this.data);
  }
  
  

  isMutiSelectMenue() { 
    if(this.desktopSvc.selectedItemList.value.length>1)
    {
      if(this.desktopSvc.selectedItemList.value.some(s=>s.id==this.data.id)){
        return true;
      }
    }
    return false;
  }



multi_copy() {
  let strList:string[] = [];
  this.desktopSvc.selectedItemList.value.forEach(s=>{
    strList.push("$ispace copy fileBase "+ s.path +" "+ s.type);
  })

  this.clipboard.copy(strList.join("\n"));
}
multi_shear() { 
  let strList:string[] = [];
  this.desktopSvc.selectedItemList.value.forEach(s=>{
    strList.push("$ispace shear fileBase "+ s.path +" "+ s.type);
  })

  this.clipboard.copy(strList.join("\n"));
}

multi_remove() {
  this.onRemove.emit(this.desktopSvc.selectedItemList.value);
}

}
